from plantmanagement import db
from plantmanagement.models import Users
from plantmanagement import settings
import requests


# 工具类
class Tools:
    def db_commit(data):
        try:
            db.session.add(data)
            db.session.commit()
            print(data)
            return 0
        except:
            return '插入失败'

    #     查询用户是否存在
    def QueryUsers(userid):
        u = Users.query.filter_by(openid=userid).first()
        if u:
            return True
        else:
            return False

    def r(datas):
        data = {
            'key': 'upkhpu3faximrmzj',
            'language': 'zh-Hans',
            'unit': 'c',
            'location': datas
        }
        url = 'https://api.seniverse.com/v3/weather/now.json'
        r = requests.get(url, params=data)
        print(r.url)
        return r.text
    def getOpenid(code):
        url = 'https://api.weixin.qq.com/sns/jscode2session?appid='+settings.APPID+'&secret='+settings.SECRET+'&js_code='+code+'&grant_type=authorization_code'
        r = requests.get(url)
        print(r.url)
        return r.text

